package com.qdxy.app.lhjh.activities.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lf.tempcore.tempFragment.TempFragment;
import com.lf.tempcore.tempModule.tempUtils.TempDensityUtil;
import com.qdxy.app.lhjh.R;
import com.qdxy.app.lhjh.bean.RespJiaGong;
import com.qdxy.app.lhjh.views.CustomExpandListview;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by KY on 2016/10/24.
 */

public class FragJiaGong2 extends TempFragment {
    private List<RespJiaGong> mJiaGongData;
    private BottomSheetDialog mButtonSheet;
    private CustomExpandListview mCustomExpandListview;
    private LayoutInflater inflater;
    private JiaGongAdapter mJiaGongAdapter;
    private final String TAG = "FragJiaGong2";
    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        Debug.info(TAG,"初始化initViews");
        return inflater.inflate(R.layout.frag_jia_gong_2_layout, null);
    }

    @Override
    protected void setListeners(View view, @Nullable Bundle savedInstanceState) {
        mCustomExpandListview = (CustomExpandListview) view.findViewById(R.id.jiagong2_listView);
    }

    @Override
    protected void bundleValues(View view, @Nullable Bundle savedInstanceState) {
//        Debug.info(TAG,"bundleValues");
        //初始化数据
        mJiaGongData = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            RespJiaGong jiaGong = new RespJiaGong();
            List<RespJiaGong.GongJian> gongJianList = new ArrayList<>();

            for (int j = 0; j < 3; j++) {
                RespJiaGong.GongJian gongjian = new RespJiaGong.GongJian();
                gongjian.setPru("WD1201060909");
                gongjian.setNum("101");
                gongJianList.add(gongjian);
            }

            jiaGong.setGongJian(gongJianList);
            jiaGong.setGongXu("工序"+i);
            mJiaGongData.add(jiaGong);
        }
        mJiaGongAdapter = new JiaGongAdapter();
        mCustomExpandListview.setDividerHeight(0);
        mCustomExpandListview.setAdapter(mJiaGongAdapter);
        mCustomExpandListview.setHeaderView(getLayoutInflater().inflate(
                R.layout.item_jia_gong_group_layout, mCustomExpandListview, false));

        mCustomExpandListview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Toast.makeText(getActivity(), "点击了第" + (i + 1) + " 类的第" + i1 + "项", Toast.LENGTH_SHORT).show();
                return true;
            }
        });


        // 默认全部展开
        for (int i = 0; i < mJiaGongData.size(); i++) {
            mCustomExpandListview.expandGroup(i);
        }
    }

    @Override
    protected void OnViewClicked(View v) {

    }
    class JiaGongAdapter extends BaseExpandableListAdapter implements CustomExpandListview.HeaderAdapter{
        public JiaGongAdapter() {
        }

        @Override
        public int getGroupCount() {

            return mJiaGongData==null?0:mJiaGongData.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return mJiaGongData.get(groupPosition)==null?0:mJiaGongData.get(groupPosition).getGongJian().size();
        }

        @Override
        public RespJiaGong getGroup(int groupPosition) {
            return mJiaGongData.get(groupPosition);
        }

        @Override
        public RespJiaGong.GongJian getChild(int groupPosition, int childPosition) {
            return getGroup(groupPosition).getGongJian().get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            JiaGongAdapter.ViewHolder viewHolder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_jia_gong_group_layout, null);
                viewHolder = new JiaGongAdapter.ViewHolder();
                viewHolder.devider =convertView.findViewById(R.id.item_jia_gong_divider);
                viewHolder.gongXu = (TextView) convertView.findViewById(R.id.item_jia_gong_gongxu);
                viewHolder.operationImage = (ImageView) convertView.findViewById(R.id.item_jia_gong_operation);
                convertView.setTag(viewHolder);
            }
            else{
                viewHolder = (JiaGongAdapter.ViewHolder) convertView.getTag();
            }
            viewHolder.gongXu.setText(getGroup(groupPosition).getGongXu());
            viewHolder.operationImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showOperationButtomSheets();
                }
            });
          /*  if (getGroupCount()>0&&groupPosition==0){
                viewHolder.devider.setVisibility(View.GONE);
            }else{
                viewHolder.devider.setVisibility(View.VISIBLE);
            }*/

            return convertView;
        }

        @Override
        public int getChildType(int groupPosition, int childPosition) {
            return super.getChildType(groupPosition, childPosition);
        }


        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            JiaGongAdapter.ViewHolder viewHolder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_jia_gong_child_layout, null);
                viewHolder = new JiaGongAdapter.ViewHolder();
                viewHolder.checkButton = (TextView) convertView.findViewById(R.id.item_jia_gong_check_btn);
                viewHolder.cardView= (CardView) convertView.findViewById(R.id.item_jia_gong_child_cardView);
                viewHolder.commitButton = (TextView) convertView.findViewById(R.id.item_jia_gong_commit_btn);
                viewHolder.devider = convertView.findViewById(R.id.item_jia_divider);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (JiaGongAdapter.ViewHolder) convertView.getTag();
            }
            viewHolder.checkButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "自检点击groupPosition="+groupPosition+"||childPosition="+childPosition, Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.commitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showChangeKnife();
                }
            });
            if (getChildrenCount(groupPosition)>0){

                if ((childPosition==(getChildrenCount(groupPosition)-1))){
//                    ViewGroup.LayoutParams lp =  convertView.getLayoutParams();

//                    convertView.setPadding(16,16,16,16);

                    viewHolder.devider.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) viewHolder.cardView.getLayoutParams();
                    lp.setMargins(TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16));
//                    viewHolder.cardView.(TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16));
                }else{
                    LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) viewHolder.cardView.getLayoutParams();
                    lp.setMargins(TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),0);

//                    viewHolder.cardView.setPadding(TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),TempDensityUtil.dip2px(getActivity(),16),0);
//                    convertView.setPadding(16,16,16,0);
                    viewHolder.devider.setVisibility(View.GONE);
                }
            }

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        @Override
        public int getHeaderState(int groupPosition, int childPosition) {
            final int childCount = getChildrenCount(groupPosition);
            if (childPosition == childCount - 1) {
                return PINNED_HEADER_PUSHED_UP;
            } else if (childPosition == -1
                    && !mCustomExpandListview.isGroupExpanded(groupPosition)) {
                return PINNED_HEADER_GONE;
            } else {
                return PINNED_HEADER_VISIBLE;
            }
        }

        @Override
        public void configureHeader(View header, int groupPosition, int childPosition, int alpha) {
            if (groupPosition > -1) {
                ((TextView) header.findViewById(R.id.item_jia_gong_gongxu))
                        .setText(getGroup(groupPosition).getGongXu());
            }
        }
        private class ViewHolder{
            public TextView gongXu;
            public View devider;
            public TextView checkButton;
            public TextView commitButton;
            public ImageView operationImage;
            private CardView cardView;
        }
    }
    protected LayoutInflater getLayoutInflater() {
//		if (inflater==null) {
//			inflater = LayoutInflater.from(context);
//		}

        return inflater == null ? LayoutInflater.from(getActivity()) : inflater;
    }
    /**
     * 更换刀具对话框
     */
    private void showChangeKnife(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(R.layout.dialog_change_knife_layout).setNegativeButton("取消",null).setPositiveButton("更换刀具",null).show();

    }
    /**
     * 显示底部导航栏
     */
    private void showOperationButtomSheets(){
        if (mButtonSheet==null){
            mButtonSheet = new BottomSheetDialog(getActivity());
            View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.buttom_sheets_jia_gong_content_layout,null);
            View cancel = contentView.findViewById(R.id.buttom_sheets_jia_gong_cancel);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mButtonSheet.dismiss();
                }
            });
            mButtonSheet.setContentView(contentView);

        }
        mButtonSheet.show();

    }
}
